<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body {
            height: 100vh;
            background: #ccc;
            overflow: hidden;
        }
        #demo {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            background: white;
            margin: auto;
        }
    </style>
</head>
<body>
    <canvas id="demo" width="600" height="600"></canvas>
</body>
<script>
    window.onload = function(){
        var canvas = document.getElementById("demo");
        if (canvas.getContext){
            var ctx = canvas.getContext("2d");
        }
        canvas.onmousedown = function (ev){
            ev = ev || event;
            if (canvas.setCapture){
                canvas.setCapture();
            }
            ctx.beginPath();
            ctx.moveTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop)
            document.onmousemove = function (ev){
                ctx.save();
                ctx.strokeStyle = "pink";
                ctx.lineTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop);
                ctx.stroke();
            }
            document.onmouseup = function (){
                document.onmousemove = document.onmouseup = null;
                if (document.releaseCapture){
                    document.releaseCapture();
                }
            }
            ctx.restore();
            return false;

        }
    }
</script>
</html>